var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {
    onOff: false
  },
  onLoad(options) {
    this._options = options;
  },
  /**
   * 切换密码显示为明文
   */
  _switchPsd: function() {
    this.setData({
      onOff: !this.data.onOff
    })
  },
  /**
   * 提交账号、验证码、密码信息
   */
  _submit(evt) {
    if (this._validate(evt.detail.value)) {
      if (this._options) {
        var paramsUrl = this._options.type == 0 ? '/public/api/user/register' : '/public/api/user/forgetPwd';
        var paramsData = {
          mobile: this._options.account,
          captcha: this._options.yzm,
          pwd: evt.detail.value.password,
          mac:app.getOpenId()
        }
        ajax.P({
          url: paramsUrl,
          data: paramsData,
          success: (res) => {
            console.log(res);
            wx.setStorageSync('uid', res.data.uid);
            if (getCurrentPages().length == 2) {
              wx.reLaunch({
                url: '/pages/home/index',
              })
            } else {
              wx.navigateBack({
                delta: 3
              })
            }
          },
          fail: (res) => {}
        })
      }
    }
  },
  /**
   * 验证输入信息
   */
  _validate(value) {
    var {
      password
    } = value;
    const reg = /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-z]|[A-Z]|[0-9]){8,16}$/;
    if (!password) {
      util.tips('密码不能为空！');
      return false;
    } else if (!reg.test(password)) {
      util.tips('密码格式不正确！');
      return false;
    }
    return true;
  }
}));